#include "Triangle.h"

#include <iostream>
#include <ctime>

namespace
  {
  double eps = 1e-6;

  bool IsIntersectStupid(const Triangle& i_a, const Segment& i_seg)
    {
    int IT = 100000;
    for (int it = 0; it <= IT; ++it)
      {
      if (i_a.IsInside(i_seg.GetPoint(1.0 * it / IT), eps))
        {
        std::cout << i_seg.GetPoint(1.0 * it / IT) << std::endl;
        return true;
        }
      }
    return false;
    }

  bool IsIntersectStupid(const Triangle& i_a, const Triangle& i_b)
    {
    for (int i = 0; i < 3; ++i)
      {
      for (int j = 0; j < 3; ++j)
        {
        if (i_a.GetSide(i).IsIntersect(i_b.GetSide(j), eps))
          return true;
        }
      }
    return IsIntersectStupid(i_a, i_b.GetSide(0))||
           IsIntersectStupid(i_a, i_b.GetSide(1))||
           IsIntersectStupid(i_a, i_b.GetSide(2))||
           IsIntersectStupid(i_b, i_a.GetSide(0))||
           IsIntersectStupid(i_b, i_a.GetSide(1))||
           IsIntersectStupid(i_b, i_a.GetSide(2));
    }
  bool Checker(int IT = 1000)
    {
    srand(static_cast<unsigned int>(time(NULL)));
    for (int i = 0; i < IT; ++i)
      {
      std::cout << "Test Case #" << i + 1 << std::endl;
      Triangle tr1, tr2;
      tr1.SetRandom(20);
      tr2.SetRandom(20);
      bool r1 = tr1.IsIntersect(tr2, 1e-8), r2 = IsIntersectStupid(tr1, tr2);
      std::cout << tr1 << std::endl;
      std::cout << tr2 << std::endl;
      std::cout << r1 << "  " << r2 << std::endl;
      if (r1 != r2)
        {
        return false;
        }
      std::cout << std::endl;
      }
    return true;
    }
  }

int main()
  {

    {
    Triangle tr1(Point3D(0, 4, 1), Point3D(-3, -5, -4), Point3D(4, 1, 4));
    Triangle tr2(Point3D(-1, -5, 1), Point3D(2, 3, -4), Point3D(2, 1, -4));
    std::cout << tr2.IsInside(Point3D(-5.0 / 3, 8.0 / 3, 4.0 / 3), 1e-8) << std::endl;
    std::cout << tr1.IsIntersect(tr2, 1e-8) << std::endl;
    std::cout << IsIntersectStupid(tr1, tr2) << std::endl;
    }

  std::cout << Checker() << std::endl;
  return 0;
  }